package io1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * @classname:InputStreamDemo
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/16 22:20
 * @version: 0.1
 * @since:1.8
 */
public class InputStreamDemo {
    public static void main(String[] args) {
//        testRead1();
//        testRead1_1();
//        testRead2();
//        testRead3();
        testRead4();
    }

    private static void testRead4() {

        try (
                InputStream in = new FileInputStream("exercise/dir/a.txt")
        ) {
            byte[] bytes = new byte[4];
            int len = 0;
            //len
            while ((len = in.read(bytes,0,3))!=-1){
                System.out.println(new String(bytes));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void testRead3() {
        //read()带byte参数的字节数组
        try (
                InputStream in = new FileInputStream("exercise/dir/a.txt")
        ) {
            //正常开发大小为1024的整数倍
            byte[] bytes = new byte[5];
            int len =0;//读取的文件内容存储到了byte

            while ((len= in.read(bytes))!=-1){
                System.out.print(new String(bytes, 0, len));

            }

            //字节数组转字符串
           // System.out.println(Arrays.toString(bytes));
            //我abc我们123
            //[-26, -120, -111, 97, 98, 99, -26, -120, -111, -28, -69, -84, 49, 50, 51, 0, 0, 0, 0, 0]
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void testRead2() {
        //try...with.resourse 1.7+提功了更加优雅的方式释放流
        try (
                //只能写流对象的创建实现Closeable接口的类的对象的创建
                InputStream in = new FileInputStream("exercise/dir/a.txt")
                //一行代码不用;
        ) {
            int len = 0;
            while ((len = in.read())!=-1){
                System.out.print((char) len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void testRead1() {
        //读取某个文件read()无参的方法
        String filePath = "exercise/dir/a.txt";
        InputStream inputStream = null;
        try {
            //创建字节输入流对象
            inputStream = new FileInputStream(filePath);
            //读取文件内容
            int read = inputStream.read();//一次读取一个字节内容
            System.out.println((char) read);
            //字节流读取字符内容有可能会出现乱码
            //汉字占两个字节会出现乱码
            //不想出现乱码（读完文件中所有的自己内容）
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void testRead1_1() {
        //读取某个文件
        String filePath = "exercise/dir/a.txt";
        InputStream inputStream = null;
        try {

            inputStream = new FileInputStream(filePath);

            int result = 0;
            while ((result = inputStream.read()) != -1) {
                System.out.print((char) result);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
